package com.swz.dcrm.adpter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.SectionIndexer;

import com.bumptech.glide.Glide;
import com.swz.dcrm.R;
import com.swz.dcrm.base.ViewHolder;
import com.swz.dcrm.model.CarBrand;
import com.swz.dcrm.util.Tool;
import com.timehop.stickyheadersrecyclerview.StickyRecyclerHeadersAdapter;


import java.util.ArrayList;
import java.util.List;

public class CarBrandAdapter extends CustomAdapter<CarBrand> implements StickyRecyclerHeadersAdapter<AddressBookAdapter.HeaderViewHolder>, SectionIndexer {

    private ArrayList<Integer> mSectionPositions;
    private List<Integer> charsIndex;
    public CarBrandAdapter(Context context,  List<CarBrand> datas) {
        super(context, R.layout.item_car_brand, datas);
    }

    @Override
    public Object[] getSections() {
        List<String> sections = new ArrayList<>(26);
        charsIndex=new ArrayList<>(26);
        List<String> chars=new ArrayList<>(26);
        mSectionPositions = new ArrayList<>(26);

        for(int i=0;i<mDatas.size();i++){
            String section =mDatas.get(i).getInitials().toUpperCase();
            if (!sections.contains(section)) {
                sections.add(section);
                mSectionPositions.add(i);
            }
        }

        for(int i=0;i<26;i++){
            char c= (char) (i+65);
            String s=c+"";
            chars.add(s);
            if(sections!=null&&sections.size()>0){
                int index=-1;
                for(int k=0;k<sections.size();k++){
                    if(sections.get(k).equals(s)){
                        index=0;
                        charsIndex.add(mSectionPositions.get(k));
                        break;
                    }
                }
                if(index==-1){
                    charsIndex.add(0);
                }
            }


        }

        return chars.toArray(new String[0]);
    }

    @Override
    public int getPositionForSection(int sectionIndex) {
        return charsIndex.get(sectionIndex);
    }

    @Override
    public int getSectionForPosition(int position) {
        return 0;
    }

    @Override
    public long getHeaderId(int position) {
        return mDatas.get(position).getInitials().charAt(0);
    }

    @Override
    public AddressBookAdapter.HeaderViewHolder onCreateHeaderViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_letter,parent,false);
        return new AddressBookAdapter.HeaderViewHolder(view);
    }

    @Override
    public void onBindHeaderViewHolder(AddressBookAdapter.HeaderViewHolder headerViewHolder, int i) {
        headerViewHolder.letter.setText(mDatas.get(i).getInitials());
    }

    @Override
    protected void convert(ViewHolder holder, CarBrand carBrand, int position) {
        Glide.with(mContext)
                .load(carBrand.getLogo())
                .into((ImageView) holder.getView(R.id.iv_brand_logo));
        holder.setText(R.id.tv_brand_name,carBrand.getName());
        holder.itemView.setOnClickListener(v -> {
            if(onClickListener!=null){
                onClickListener.onItemClick(carBrand);
            }
        });
    }
}
